package base.httpclient.jsoup;

import org.apache.commons.io.FileUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Attributes;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;

/**
 * jsoup 专业负责解析， HttpClient专业负责抓取
 */
public class TestCase {
    public static void main(String[] args) {
//        testURL();
//        testString();
//        testFile();
//        testFile2();
        testSelect();
    }

    // 解析URL地址
    public static void testURL(){
        try {
            Document document = Jsoup.parse(new URL("http://www.itcast.cn"), 1000);
            String title = document.getElementsByTag("title").first().text();
            System.out.println(title);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 解析字符串
    public static void testString(){
        try {
            String s = FileUtils.readFileToString(new File("src/test/java/test.html"), StandardCharsets.UTF_8);

            Document doc = Jsoup.parse(s);
            String title = doc.getElementsByTag("title").first().text();
            System.out.println(title);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 解析文件
    public static void testFile(){
        try {
            Document doc = Jsoup.parse(new File("src/test/java/test.html"), StandardCharsets.UTF_8.name());
            String title = doc.getElementsByTag("title").first().text();
            System.out.println(title);
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    public static void testFile2(){
        try {
            Document doc = Jsoup.parse(new File("src/test/java/test.html"), StandardCharsets.UTF_8.name());
            Element element = doc.getElementById("sexnan");
            String value = element.attr("value");
            System.out.println(value);

            Attributes attributes = element.attributes();
            System.out.println(attributes);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void testSelect(){
        try {
            Document doc = Jsoup.parse(new File("src/main/resources/html/test.html"), StandardCharsets.UTF_8.name());

            // 标签选择器
            Elements span = doc.select("span");
            for (Element element : span) {
//                System.out.println(element.text());
            }

            // id选择器
            Element span1 = doc.select("#span1").first();
            System.out.println(span1.text());

            // class 选择器
            Element span2 = doc.select(".span1").first();
            System.out.println(span2.text());

            // 属性选择器
            Element e = doc.select("[value]").first();
            System.out.println(e.text());//输出为空， e = <input type="radio" name="one" value="男" id="sexnan">

            // 属性值选择器
            Element nan = doc.select("[value=男]").first();
            System.out.println(nan);//输出 <input type="radio" name="one" value="男" id="sexnan">
            String id = nan.attr("id");
            System.out.println(id);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
